﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace Event_SG_EWDT
{
    public partial class UpdateEvent : System.Web.UI.Page
    {
        string eventid = "";
        protected void Page_Load(object sender, EventArgs e)
        {

            eventid = Request.QueryString["eventid"];
            Session["Event"] = eventid;

            if (Session["Member"] == null || eventid == "")
            {
                Session["flag"] = "redirecterror";
                Response.Redirect("~/registration.aspx");
            }

            if (!EventControl.IsAdmin(Session["Member"].ToString(), eventid))
            {
                Session["flag"] = "notauthorizedevent";
                Response.Redirect("~/registration.aspx");
            }

            if (!IsPostBack)
            {
                TabContainer1.ActiveTabIndex = 0;
                if (eventid != "")
                {
                    Event ev = EventControl.RetrieveEvent(eventid);

                    if (ev.EventId == null)
                    {
                        Session["flag"] = "notfound";
                        Response.Redirect("~/index.aspx");
                    }

                    tbxUpdateTitle.Text = ev.EventTitle;
                    tbxUpdateDesc.Text = ev.Description;
                    tbxUpdateSdate.Text = ev.StartTime.ToString("yyyy/MM/dd");
                    tbxUpdateEdate.Text = ev.EndTime.ToString("yyyy/MM/dd");
                    tbxUpdateLocation.Text = ev.Location;
                    tbxUpdateFee.Text = ev.EventFee.ToString();
                    DissectTime(ev.StartTime, tbxUpdateSHour, tbxUpdateSMinute, tbxUpdateSAMPM);
                    DissectTime(ev.EndTime, tbxUpdateHour, tbxUpdateMinute, tbxUpdateAMPM);

                    if (ev.Privacy == 0)
                    {
                        rdblUpdatePrivacy.SelectedIndex = 0;
                    }
                    else if (ev.Privacy == 1)
                    {
                        rdblUpdatePrivacy.SelectedIndex = 1;
                    }
                    else if (ev.Privacy == 2)
                    {
                        rdblUpdatePrivacy.SelectedIndex = 2;
                    }
                    ddlUpdateCat.SelectedValue = ev.CategoryId.ToString();
                }
            }
            if (!IsPostBack)
            {
                TabContainer1.ActiveTabIndex = 0;
            }

            rvStartDateU.MinimumValue = DateTime.Now.ToShortDateString();
            rvStartDateU.MaximumValue = DateTime.Now.AddYears(5).ToShortDateString();
            rvEndDateU.MinimumValue = DateTime.Now.ToShortDateString();
            rvEndDateU.MaximumValue = DateTime.Now.AddYears(5).ToShortDateString();
        }

        protected void btnUpdateEvent_Click(object sender, EventArgs e)
        {
            DateTime startTime = GetTime(tbxUpdateSHour.Text, tbxUpdateSMinute.Text, tbxUpdateSAMPM.Text, Convert.ToDateTime(tbxUpdateSdate.Text).Year, Convert.ToDateTime(tbxUpdateSdate.Text).Month, Convert.ToDateTime(tbxUpdateSdate.Text).Day);

            DateTime endTime = GetTime(tbxUpdateSHour.Text, tbxUpdateSMinute.Text, tbxUpdateSAMPM.Text, Convert.ToDateTime(tbxUpdateEdate.Text).Year, Convert.ToDateTime(tbxUpdateEdate.Text).Month, Convert.ToDateTime(tbxUpdateEdate.Text).Day);

            //***Need to throw up the error so we know we encountered ex, so we can redirect or show notifications to user

            bool up = EventControl.UpdateEvent(eventid, tbxUpdateTitle.Text, tbxUpdateDesc.Text, startTime, endTime, Convert.ToDouble(tbxUpdateFee.Text), Convert.ToInt32(ddlUpdateCat.SelectedValue), tbxUpdateLocation.Text, rdblUpdatePrivacy.SelectedIndex);

            if (fuUpdatePoster.HasFile)
            {
                Utility.UploadEventPoster(fuUpdatePoster, eventid);
            }


            if (fuUpdateThumbnail.HasFile)
            {
                Utility.UploadEventThumbnail(fuUpdateThumbnail, eventid);
            }

            //Utility.UploadEventPoster(Server, fuUpdatePoster, eventid);
            //Utility.UploadEventThumbnail(Server, fuUpdateThumbnail, eventid);

            if (up)
            {

                //Notifications

                Utility.SendUpdateNotificationToEventMembers(Request, Convert.ToInt32(eventid));


                Session["flag"] = "updated";
                Response.Redirect("~/ViewEvent.aspx?eventid=" + eventid);
            }
            else
            {
                ScriptManager.RegisterStartupScript(this, this.GetType(), "toast", "$().toastmessage('showWarningToast', 'Opps! One or more required fields were invald!');", true);
            }
        }
        DateTime GetTime(string hour, string minute, string ampm, int year, int month, int day)
        {
            int modifier = 0;

            switch (ampm)
            {
                case "AM":
                    modifier = 0;
                    break;
                case "PM":
                    modifier = 12;
                    break;
            }


            return new DateTime(year, month, day, Convert.ToInt32(hour) + modifier, Convert.ToInt32(minute), 0);
        }

        void DissectTime(DateTime dt, TextBox tbxhr, TextBox tbxmin, TextBox tbxmod)
        {
            DateTime st = dt;

            string mod = "";
            int hr = st.Hour;

            int min = st.Minute;
            string m = st.Minute.ToString();

            if (st.Hour < 12)
            {
                mod = "AM";
            }
            else
            {
                mod = "PM";
                hr -= 12;
            }

            if (min < 10)
            {
                m = "0" + min.ToString();
            }

            tbxhr.Text = hr.ToString();
            tbxmin.Text = m.ToString();
            tbxmod.Text = mod;
        }

        protected void gvWhoIsComingEvents_SelectedIndexChanged(object sender, EventArgs e)
        {
            string id = gvWhoIsComingEvents.SelectedRow.Cells[0].Text;
            EventControl.UpdateMemberEventStatus(eventid, id, 1);
            gvWhoIsComingEvents.DataBind();
        }

        protected void gvPendingEventList_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            int index = Convert.ToInt32(e.CommandArgument);
            GridViewRow row = gvPendingEventList.Rows[index];
            if (e.CommandName == "Accept")
            {
                EventControl.EventApproval(row.Cells[0].Text, row.Cells[2].Text, 0);
                Utility.SendAcceptedToEvent(Convert.ToInt32(Session["Member"]), Convert.ToInt32(row.Cells[0].Text), row.Cells[2].Text);
            }
            else if (e.CommandName == "Reject")
            {
                MemberEvent me = new MemberEvent();
                me.MembershipNo = row.Cells[0].Text;
                me.EventId = row.Cells[2].Text;
                DBMANAGER.DeleteMemberEvent(me);

            }
            gvPendingEventList.DataBind();
            gvWhoIsComingEvents.DataBind();
        }
    }
}
